<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="水费" prop="waterFare">
        <el-input
          v-model.number="dataForm.waterFare"
          placeholder="水费"
        ></el-input>
      </el-form-item>
      <el-form-item label="电费" prop="etcFare">
        <el-input
          v-model.number="dataForm.etcFare"
          placeholder="电费"
        ></el-input>
      </el-form-item>
      <el-form-item label="总计" prop="sum">
        <el-input v-model="dataForm.sum" placeholder="总计"></el-input>
      </el-form-item>
      <el-form-item label="用水量(L)" prop="waterCount">
        <el-input
          v-model="dataForm.waterCount"
          placeholder="用水量(L)"
        ></el-input>
      </el-form-item>
      <el-form-item label="用电量（度）" prop="etcCount">
        <el-input
          v-model="dataForm.etcCount"
          placeholder="用电量（度）"
        ></el-input>
      </el-form-item>
      <el-form-item label="水费单价" prop="waterPrice">
        <el-input
          v-model="dataForm.waterPrice"
          placeholder="水费单价"
        ></el-input>
      </el-form-item>
      <el-form-item label="电费单价" prop="etcPrice">
        <el-input v-model="dataForm.etcPrice" placeholder="电费单价"></el-input>
      </el-form-item>
      <el-form-item label="宿舍id" prop="dormId">
        <!-- <el-input v-model="dataForm.dormId" placeholder="宿舍id"></el-input> -->
        <!-- <el-select v-model="dataForm.dormId" placeholder="请选择宿舍号">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select> -->
        <el-cascader
          :props="defaultProps"
          v-model="dormValue"
          :options="menus"
          @change="handleChange"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="年_月" prop="month">
        <!-- <el-input v-model="dataForm.month" placeholder="年_月"></el-input> -->
        <el-date-picker
          v-model="dataForm.month"
          type="month"
          placeholder="选择月份"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="缴费状态（0:未交 1：已交）" prop="status">
        <el-input
          v-model="dataForm.status"
          placeholder="缴费状态（0:未交 1：已交）"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-input
          v-model="dataForm.createTime"
          placeholder="创建时间"
        ></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      flag: 0,
      defaultProps: {
        children: "children",
        label: "name",
        value: "id",
      },
      dormValue: "",
      menus: [],
      // value: "",
      options: [],
      visible: false,
      dataForm: {
        id: 0,
        waterFare: "",
        etcFare: "",
        sum: "",
        waterCount: "",
        etcCount: "",
        waterPrice: "",
        etcPrice: "",
        dormId: "",
        month: "",
        status: 0,
        createTime: "",
      },
      dataRule: {
        waterFare: [
          { required: true, message: "水费不能为空", trigger: "blur" },
        ],
        etcFare: [{ required: true, message: "电费不能为空", trigger: "blur" }],
        sum: [{ required: true, message: "总计不能为空", trigger: "blur" }],
        waterCount: [
          { required: true, message: "用水量(L)不能为空", trigger: "blur" },
        ],
        etcCount: [
          { required: true, message: "用电量（度）不能为空", trigger: "blur" },
        ],
        waterPrice: [
          { required: true, message: "水费单价不能为空", trigger: "blur" },
        ],
        etcPrice: [
          { required: true, message: "电费单价不能为空", trigger: "blur" },
        ],
        // dormId: [
        //   { required: true, message: "宿舍id不能为空", trigger: "blur" },
        // ],
        month: [{ required: true, message: "年_月不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "缴费状态（0:未交 1：已交）不能为空",
            trigger: "blur",
          },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getMenus();
  },
  activated() {
    // this.getDormIds();
  },
  watch: {
    "dataForm.waterFare": {
      handler(val) {
        this.dataForm.sum = this.dataForm.waterFare + this.dataForm.etcFare;
      },
    },
    "dataForm.etcFare": {
      handler(val) {
        this.dataForm.sum = this.dataForm.waterFare + this.dataForm.etcFare;
      },
    },
  },
  mounted() {
    this.getDormIds();
  },
  methods: {
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/sys/dormarea/getCategory"),
        method: "get",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.menus = data.data;
      });
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/dormfare/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.waterFare = data.dormFare.waterFare;
              this.dataForm.etcFare = data.dormFare.etcFare;
              this.dataForm.sum = data.dormFare.sum;
              this.dataForm.waterCount = data.dormFare.waterCount;
              this.dataForm.etcCount = data.dormFare.etcCount;
              this.dataForm.waterPrice = data.dormFare.waterPrice;
              this.dataForm.etcPrice = data.dormFare.etcPrice;
              this.dataForm.dormId = data.dormFare.dormId;
              this.dataForm.month = data.dormFare.month;
              this.dataForm.status = data.dormFare.status;
              this.dataForm.createTime = data.dormFare.createTime;
            }
          });
        }
      });
    },
    getDormIds() {
      this.$http({
        url: this.$http.adornUrl("/sys/dormdetail/getDormIds"),
        method: "get",
      }).then(({ data }) => {
        // alert(data.data)
        this.options = [];
        if (data && data.code === 0) {
          for (let i in data.data) {
            let a = {};
            a.name = "value";
            a.value = data.data[i];
            a.name = "label";
            a.value = data.data[i];
            this.options.push(a);
          }
        } else {
          this.dataList = [];
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            this.dataForm.status = 0;
            // this.dataForm.month=this.dataForm.month.substring(0,7);
            var date = this.dataForm.month;
            var Y = date.getFullYear() + "-";
            var M =
              date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1;
            this.dataForm.month = Y + M;
          }
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dormfare/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              waterFare: this.dataForm.waterFare,
              etcFare: this.dataForm.etcFare,
              sum: this.dataForm.sum,
              waterCount: this.dataForm.waterCount,
              etcCount: this.dataForm.etcCount,
              waterPrice: this.dataForm.waterPrice,
              etcPrice: this.dataForm.etcPrice,
              dormId: this.dormValue[2],
              month: this.dataForm.month,
              status: this.dataForm.status,
              createTime: this.dataForm.createTime,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
